<template>
    <el-dialog :close-on-click-modal="false" ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible"
        width="50%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="区域名称" prop="areaId">
                <el-select class="w100p" v-model="ruleForm.areaId" filterable clearable placeholder="请选择父级区域">
                    <el-option v-for="item in areaList" :key="item.areaId" :label="item.areaName" :value="item.areaId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="抄表类型" prop="energyId">
                <el-select class="w100p" v-model="ruleForm.energyId" placeholder="请选择能源类型" filterable clearable
                    @change="getUnit">
                    <el-option v-for="item in energyList" :key="item.energyId" :label="item.energyName"
                        :value="item.energyId" />
                </el-select>
            </el-form-item>
            <el-form-item label="累计用量" prop="equipmentValue">
                <el-input class="w100p" type="number" v-model="ruleForm.equipmentValue" clearable
                    placeholder="请输入用量"><template slot="append">{{ ruleForm.energyUnit }}</template></el-input>
            </el-form-item>
            <el-form-item label="抄表人" prop="recordUserNo">
                <el-select class="w100p" v-model="ruleForm.recordUserNo" placeholder="请选择能源类型" filterable clearable>
                    <el-option v-for="item in personList" :key="item.personNo" :label="item.name"
                        :value="item.personNo" />
                </el-select>
            </el-form-item>
            <el-form-item label="抄表时间" prop="recordTime">
                <el-date-picker class="w100p" v-model="ruleForm.recordTime" value-format="yyyy-MM-dd HH:mm:ss"
                    format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间" :picker-options="pickerOptions">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import manualMeterApi from '@/api/manualMeter'
import { formatDate } from "@/utils/filterTime"
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        personList: {
            type: Array,
            default: () => {
                return []
            }
        },
        areaList: {
            type: Array,
            default: () => {
                return []
            }
        },
        energyList: {
            type: Array,
            default: () => {
                return []
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                }
            },
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                recordTime: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ],
                recordUserNo: [
                    { required: true, message: '请选择记录人', trigger: 'change' }
                ],
                energyId: [
                    { required: true, message: '请选择能源类型', trigger: 'change' }
                ],
                areaId: [
                    { required: true, message: '请选择区域', trigger: 'change' }
                ],
                equipmentValue: [
                    { required: true, message: '请输入用量', trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = manualMeterApi.add
                this.addEditName = '添加';
                let person = this.personList.find(e => e.personNo == sessionStorage.personNo);
                this.$set(this.ruleForm, 'recordUserNo', person ? person.personNo : '')
                this.$set(this.ruleForm, 'recordTime', formatDate(new Date()))
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = manualMeterApi.edit
                this.addEditName = '编辑'
            }
        },
        getUnit(id) {
            this.ruleForm.energyUnit = this.getName(this.energyList, id, 'energyId', 'energyUnit')
        },
        getName(list, id, key, name) {
            let rut = list.find(e => e[key] == id)
            return rut ? rut[name] : ""
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({
                        ...this.ruleForm,
                        areaName: this.getName(this.areaList, this.ruleForm.areaId, 'areaId', 'areaName'),
                        energyName: this.getName(this.energyList, this.ruleForm.energyId, 'energyId', 'energyName'),
                        energyUnit: this.getName(this.energyList, this.ruleForm.energyId, 'energyId', 'energyUnit'),
                        recordUser: this.getName(this.personList, this.ruleForm.recordUserNo, 'personNo', 'name'),
                    }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>